package com.example.yk2021xm04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yk2021xm04.pojo.Process;
import com.example.yk2021xm04.mapper.ProcessMapper;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.ProcessQuery;
import com.example.yk2021xm04.service.IProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yk2021xm04.utils.AssertUtil;
import com.example.yk2021xm04.utils.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 * </p>
 *
 * @author ZengQi
 * @since 2022-01-20
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process> implements IProcessService {

    @Override
    public void addProcess(Process process) {
        AssertUtil.isTrue(StringUtils.isBlank(process.getProcessName()), "进度名称不能为空");
        process.setUploadTime(LocalDateTime.now());
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        process.setSubmitter(user.getId());
        AssertUtil.isTrue(!(this.save(process)),"进度记录添加失败");
    }

    @Override
    public Map<String, Object> processList(ProcessQuery processQuery) {
        IPage<Process> page = new Page<Process>(processQuery.getPage(),processQuery.getLimit());
        QueryWrapper<Process> queryWrapper = new QueryWrapper<Process>();

        if(StringUtils.isNotBlank(processQuery.getProcessName())){
            queryWrapper.like("process_name",processQuery.getProcessName());
        }

        page = this.baseMapper.selectPage(page,queryWrapper);
        System.out.println(page.getTotal());
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteProcess(Integer[] ids) {
        AssertUtil.isTrue(null == ids || ids.length == 0, "请选择待删除的id");
        AssertUtil.isTrue(!(this.removeByIds(Arrays.asList(ids))),"进度记录删除失败");
    }

    @Override
    public void updateProcess(Process process) {
        AssertUtil.isTrue(StringUtils.isBlank(process.getProcessName()),"进度标题不能为空");
        process.setUploadTime(LocalDateTime.now());
        Process temp = this.findProcessByProcessName(process.getProcessName());
        AssertUtil.isTrue(null!=temp&&!(temp.getId().equals(process.getId())),"进度名已存在！");
        AssertUtil.isTrue(!(this.updateById(process)),"进度更新失败");

    }

    @Override
    public List<Object> uploadTimeByDay() {
        return this.baseMapper.uploadTimeByDay();
    }

    @Override
    public List<Object> uploadCountByDay() {
        return this.baseMapper.uploadCountByDay();
    }

    @Override
    public Integer ProcessCountInDay() {
        return this.baseMapper.ProcessCountInDay();
    }

    @Override
    public Integer ProcessCountInYesterday() {
        return this.baseMapper.ProcessCountInYesterday();
    }

    private Process  findProcessByProcessName(String processName){
        return this.baseMapper.selectOne(new QueryWrapper<Process>().eq("process_name",processName));
    }
}
